package im.elephas.androfan.activity;

import im.elephas.androfan.R;
import im.elephas.androfan.common.widget.LineWidget;
import im.elephas.androfan.common.widget.MoreWidget;
import im.elephas.androfan.common.widget.MyStatusWidget;
import im.elephas.androfan.common.widget.RefreshWidget;
import im.elephas.androfan.common.widget.ReplyStatusWidget;
import im.elephas.androfan.common.widget.SendWidget;
import im.elephas.androfan.common.widget.SendWidget.SendOpration;
import im.elephas.androfan.common.widget.StatusWidget;
import im.elephas.androfan.fanfouapi.events.IStatusesEvent;
import im.elephas.androfan.fanfouapi.items.StatusItem;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.content.res.Configuration;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public abstract class StatusActivity extends AndroFanActivity implements
		SensorEventListener {

	protected boolean isGettingStatuses = false;

	protected LinearLayout linearLayout;
	protected ScrollView scrollView;

	protected List<StatusItem> listStatusItems;

	protected int count = 20;

	private SensorManager mSensorManager;
	private int sensorEventCount = 0;
	private float ox, oy, oz;
	private boolean canDetectSensor = true;

	protected IStatusesEvent mIStatusesEvent = new IStatusesEvent() {

		@Override
		public void statusesHandler(List<StatusItem> lsi) {
			listStatusItems = lsi;
			getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					refreshStatuses();
				}
			});
		}
	};

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (!isGettingStatuses)
			refreshStatuses();
	}

	protected void refreshStatuses() {
		isGettingStatuses = false;

		setContentView(R.layout.statuses);

		linearLayout = (LinearLayout) findViewById(R.id.statusesLinearLayout);
		scrollView = (ScrollView) findViewById(R.id.statusesScrollView);
		
		setRefreshWidget();
		
		setLineWidget();

		if (listStatusItems.size() <= 50) {
			for (StatusItem item : listStatusItems) {
				getStatusWidget(item);
				setLineWidget();
			}
		} else {
			for (int i = 0; i < 40; i++) {
				getStatusWidget(listStatusItems.get(i));
				setLineWidget();
			}
			for (int i = listStatusItems.size() - 10; i < listStatusItems
					.size(); i++) {
				getStatusWidget(listStatusItems.get(i));
				setLineWidget();
			}
		}

		setMoreWidget();
	}

	protected abstract void getStatuses();

	protected abstract void getMoreStatuses();

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (canDetectSensor) {
			float[] s = event.values;
			if (Math.abs(s[0] - ox) >= 7 || Math.abs(s[1] - oy) >= 7
					|| Math.abs(s[2] - oz) >= 7) {
				sensorEventCount++;
			} else if (sensorEventCount >= 0)
				sensorEventCount--;
			ox = s[0];
			oy = s[1];
			oz = s[2];

			if (sensorEventCount >= 3) {
				sensorEventCount = 0;
				if (isGettingStatuses == false) {
					isGettingStatuses = true;
					canDetectSensor = false;
					Timer timer = new Timer();
					timer.schedule(new TimerTask() {

						@Override
						public void run() {
							canDetectSensor = true;
						}
					}, 5000);
					setContentView(R.layout.statusesfreshing);
					getStatuses();
				}
			}
		}

	}

	protected StatusWidget getStatusWidget(StatusItem item) {
		StatusWidget sw;
		if (item.getUser().getId().equals(getFanfouApi().getUser().getId()))
			sw = new MyStatusWidget(linearLayout, getActivity(), item);
		else if (item.getInReplyToStatusId() != "")
			sw = new ReplyStatusWidget(linearLayout, getActivity(), item);
		else
			sw = new StatusWidget(linearLayout, getActivity(), item);
		sw.addToParentView();
		return sw;
	}

	protected RefreshWidget setRefreshWidget() {
		RefreshWidget rw = new RefreshWidget(linearLayout, getActivity(),
				new RefreshWidget.RefreshOpration() {

					@Override
					public void doRefresh() {
						if (!isGettingStatuses)
							getStatuses();
					}
				});
		rw.addToParentView();
		return rw;
	}

	protected MoreWidget setMoreWidget() {
		MoreWidget mw = new MoreWidget(linearLayout, getActivity(),
				new MoreWidget.MoreOpration() {

					@Override
					public void doMore() {
						getMoreStatuses();
					}
				});

		mw.addToParentView();
		return mw;
	}

	protected void setLineWidget() {
		LineWidget lw = new LineWidget(linearLayout, getActivity());
		lw.addToParentView();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statusesfreshing);

		setTitle();

		isGettingStatuses = true;
		getStatuses();

		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		mSensorManager.registerListener(this, mSensorManager
				.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_UI);
	}

	protected abstract void setTitle();

}
